package com.ky.study.controller;


import cn.hutool.core.convert.NumberWithFormat;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ky.common.config.SystemConfig;
import com.ky.common.util.JwtUtil;
import com.ky.common.vo.R;
import com.ky.study.entity.Article;
import com.ky.study.entity.Review;
import com.ky.study.mapper.ArticleMapper;
import com.ky.study.mapper.ReviewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author wgz
 * @since 2024-06-17
 */
@RestController
@RequestMapping("/study/review")
public class ReviewController {
    @Autowired
    ReviewMapper reviewMapper;
    @Autowired
    ArticleMapper articleMapper;
    @RequestMapping("/comment")
    public R comment(HttpServletRequest request, @RequestParam("articleId") Integer articleId, @RequestParam("content") String content){
        Review review = new Review();
        review.setArticleId(articleId);
        review.setContent(content);
        review.setReviewTime(new java.util.Date());
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        review.setUserId(id);
        int insert = reviewMapper.insert(review);
        if (insert==1){
        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
        articleQueryWrapper.eq("article_id", articleId);
        Article article = articleMapper.selectOne(articleQueryWrapper);
        article.setCommentsCount(article.getCommentsCount()+1);
        articleMapper.update(article,articleQueryWrapper);
        return R.ok();
        }
        return R.fail("评论失败");
    }
    //查找评论
    @RequestMapping("/findAll/{articleId}")
    public R findAll(@PathVariable("articleId") Integer articleId){

        QueryWrapper<Review> reviewQueryWrapper = new QueryWrapper<>();
        reviewQueryWrapper.eq("article_id",articleId);
        return R.ok(reviewMapper.selectList(reviewQueryWrapper));
    }





















    @RequestMapping("/findProblem")
    public R findProblem(@RequestParam("problemId") Integer problemId){
        QueryWrapper<Review> reviewQueryWrapper = new QueryWrapper<>();
        reviewQueryWrapper.eq("problem_id",problemId);
        return R.ok(reviewMapper.selectList(reviewQueryWrapper));
    }


    @RequestMapping("/commentProblem")
    public R commentProblem(HttpServletRequest request, @RequestParam("problemId") Integer problemId, @RequestParam("content") String comment){
        Review review2 = new Review();
        review2.setProblemId(problemId);
        review2.setContent(comment);
        review2.setReviewTime(new Date());
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        review2.setUserId(id);
        int insert = reviewMapper.insert(review2);
        if (insert > 0){
            return R.ok(review2);
        }
        return R.fail("评论失败");
    }


    @RequestMapping("/findCircle")
    public R findCircle(@RequestParam("circleArticleId") Integer circleArticleId){
        QueryWrapper<Review> reviewQueryWrapper = new QueryWrapper<>();
        reviewQueryWrapper.eq("circle_article_id",circleArticleId);
        return R.ok(reviewMapper.selectList(reviewQueryWrapper));
    }


    @RequestMapping("/commentCircle")
    public R commentCircle(HttpServletRequest request, @RequestParam("circleArticleId") Integer circleArticleId, @RequestParam("content") String comment){
        Review review1 = new Review();
        review1.setCircleArticleId(circleArticleId);
        review1.setContent(comment);
        review1.setReviewTime(new Date());
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        review1.setUserId(id);
        int insert = reviewMapper.insert(review1);
        if (insert > 0){
            return R.ok(review1);
        }
        return R.fail("评论失败");
    }

}

